#pragma once

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <string>
#include <cstring>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

class UdpClient{
private:
    int sockfd;
    std::string server_ip;
    int server_port;
public:
    UdpClient(std::string _ip, int _port)
      :server_ip(_ip),
      server_port(_port){

      }

    bool InitUdpClient()
    {
        sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        if(sockfd < 0){
            std::cerr << "socket error" << std::endl;
            return false;
        }
        // 客户端可以不绑定（端口唯一即可，不需要和进程强相关），调用接口自动分配（sendto...）
        
        return true;
    }

    void Start()
    {
        struct sockaddr_in peer;
        memset(&peer, 0 , sizeof(peer));

        peer.sin_family = AF_INET;
        peer.sin_port = htons(server_port);
        peer.sin_addr.s_addr = inet_addr(server_ip.c_str());

        std::string msg;
        while(1){
            std::cout << "Please Enter#";
            std::cin >> msg;
            sendto(sockfd, msg.c_str(), msg.size(), 0, (struct sockaddr*)&peer, sizeof(peer) );

            char buffer[128];
            ssize_t size = recvfrom(sockfd, buffer, sizeof(buffer)-1, 0, NULL, NULL);
            if(size > 0){
                buffer[size] = '\0';
                std::cout << buffer << std::endl;
            }
        }
    

    }
    ~UdpClient()
    {
        if(sockfd >= 0)
            close(sockfd);
    
    }
};
